/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HomeUtama.java
 *
 * Created on Jun 19, 2013, 8:24:56 AM
 */
package View;

import Controller.ControlData;
import Model.Operator;
import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author a9uszT
 */
public class HomeUtama extends javax.swing.JFrame {

    /** Creates new form HomeUtama */
    public HomeUtama() {
        initComponents();
        Clock clock = new Clock();
        clock.showDigitalClock(time);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        time = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        usernameTF = new javax.swing.JTextField();
        masuk_button2 = new javax.swing.JButton();
        password_TF = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        button_upload_DB = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PIcture/header2.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 868, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 870, 180));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        time.setFont(new java.awt.Font("Tahoma", 1, 14));
        time.setText("time");
        jPanel3.add(time, new org.netbeans.lib.awtextra.AbsoluteConstraints(720, 10, -1, -1));

        jPanel6.setBackground(new java.awt.Color(204, 204, 204));
        jPanel6.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel6.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel13.setText("LOGIN");
        jPanel6.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 30, 61, 27));

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel14.setText("Kode OP");
        jPanel6.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, -1, -1));
        jPanel6.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 146, -1, -1));

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel16.setText("Password");
        jPanel6.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 138, -1, -1));

        usernameTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                usernameTFKeyReleased(evt);
            }
        });
        jPanel6.add(usernameTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(105, 100, 139, -1));

        masuk_button2.setFont(new java.awt.Font("Tahoma", 1, 14));
        masuk_button2.setText("Masuk");
        masuk_button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                masuk_button2ActionPerformed(evt);
            }
        });
        jPanel6.add(masuk_button2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 190, 89, -1));

        password_TF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                password_TFKeyReleased(evt);
            }
        });
        jPanel6.add(password_TF, new org.netbeans.lib.awtextra.AbsoluteConstraints(105, 138, 139, -1));

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PIcture/images.jpg"))); // NOI18N
        jPanel6.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 290, 240));

        jPanel3.add(jPanel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 70, 290, 240));

        button_upload_DB.setText("Upload Database");
        button_upload_DB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_upload_DBActionPerformed(evt);
            }
        });
        jPanel3.add(button_upload_DB, new org.netbeans.lib.awtextra.AbsoluteConstraints(720, 60, -1, -1));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PIcture/wall1.jpg"))); // NOI18N
        jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 360));

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 180, 870, 360));

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void masuk_button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_masuk_button2ActionPerformed
        String a = usernameTF.getText().toUpperCase();
        String b = password_TF.getText();
        if (a.equalsIgnoreCase("") && b.equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(rootPane, "inputan tidak lengkap \n ID dan password kosong"
                    + "", "peringatan", JOptionPane.ERROR_MESSAGE);
            password_TF.setBackground(Color.red);
            usernameTF.setBackground(Color.red);
        } else if (!a.equalsIgnoreCase("") && b.equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(rootPane, "inputan tidak lengkap \n  password kosong"
                    + "", "peringatan", JOptionPane.ERROR_MESSAGE);
            password_TF.setBackground(Color.red);
        } else if (a.equalsIgnoreCase("") && !b.equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(rootPane, "inputan tidak lengkap \n ID kosong"
                    + "", "peringatan", JOptionPane.ERROR_MESSAGE);
            usernameTF.setBackground(Color.red);
        } else if (a.matches("[0-9]*")) {
            JOptionPane.showMessageDialog(rootPane, "ID tidak valid"
                    + "", "peringatan", JOptionPane.ERROR_MESSAGE);
            usernameTF.setBackground(Color.red);
        } else {
            String id = usernameTF.getText();
            String passwd = password_TF.getText();
            try {
                boolean cek = ControlData.getKoneksi().Login(id, passwd);
                if (cek == true) {
                    FrameOperator FP = new FrameOperator();
                    FP.setVisible(true);
                    emptyField();
                    this.dispose();
                    Operator op = ControlData.getKoneksi().cariPegawai(id);
                    FP.dataOperator(op.getNamaOperator(), op.getKode_operator());

                } else {
                    JOptionPane.showMessageDialog(rootPane, "username dan password salah\n silahkan username dan "
                            + "password lagi");
                    emptyField();
                    usernameTF.requestFocus();

                }
            } catch (Exception ex) {
//                JOptionPane.showMessageDialog(rootPane, "Tidak dapat koneksi ke Database \n"
//                        + "cek settingan dahulu");
                JOptionPane.showMessageDialog(rootPane, "Database belum ada, silahkan upload database dahulu");
            }
        }
    }//GEN-LAST:event_masuk_button2ActionPerformed

    private void usernameTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameTFKeyReleased
        usernameTF.setBackground(Color.white);
        // TODO add your handling code here:
    }//GEN-LAST:event_usernameTFKeyReleased

    private void password_TFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_password_TFKeyReleased
        password_TF.setBackground(Color.white);
        // TODO add your handling code here:
    }//GEN-LAST:event_password_TFKeyReleased

    private void button_upload_DBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_upload_DBActionPerformed
        sqldump sql=new sqldump();
        sql.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_button_upload_DBActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new HomeUtama().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton button_upload_DB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JButton masuk_button2;
    private javax.swing.JPasswordField password_TF;
    private javax.swing.JLabel time;
    public static javax.swing.JTextField usernameTF;
    // End of variables declaration//GEN-END:variables

    private void emptyField() {
        usernameTF.setText("");
        password_TF.setText("");
    }
}
